import fetch from '@/lib/http'
import {baseURI} from '@/config/env'
import {FIDE_CREDIT_API_URL, FIDE_CREDIT_API_CONFIG} from '@/config/settings'
import CryptoJS from 'crypto-js'

let qs = require('qs')

//post url
const parseUrl = (url) => {
    return baseURI + CryptoJS.AES.encrypt(FIDE_CREDIT_API_URL[url]['server'] + FIDE_CREDIT_API_URL[url]['url'], 'fide')
}

//get
const parseGetUrl = (url, params) => {
    return url + "?" + Object.keys(params).map(key => {
        return encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
    }).join('&');

}



const parseParams = (params) => {
    var AesData = CryptoJS.AES.encrypt(JSON.stringify(params), 'fide')
    var base64Data = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(AesData.toString()))

    return qs.stringify({
        cdata: base64Data
    })
}


var http = {}

//post请求
http.post = (url, params) => {
    // params.orgCode = FIDE_CREDIT_API_CONFIG.orgCode
    return fetch({
        url: url,
        method: 'post',
        data: parseParams(params)
    })
}

//get请求
http.get = (url, params) => {
    // params.orgCode = FIDE_CREDIT_API_CONFIG.orgCode
    return fetch({
        url: parseGetUrl(parseUrl(url), params),
        method: 'get',
        isLoading: false
    })
}

export {
    http
}
